import Cookies from 'js-cookie'

const TokenKey = 'yxzdsw_pc_token'

export function getToken() {
	return Cookies.get(TokenKey)
}

export function setToken(token) {
	return Cookies.set(TokenKey, token)
}

export function removeToken() {
	return Cookies.remove(TokenKey)
}

export function getStorageToken() {
	return window.localStorage.getItem(TokenKey);
}

export function setStorageToken(token) {
	return window.localStorage.setItem(TokenKey, token)
}

export function removeStorageToken() {
	return window.localStorage.removeItem(TokenKey)
}

export function isWeiXin() { //判断是否是微信环境
	var ua = window.navigator.userAgent.toLowerCase();
	if(ua.match(/MicroMessenger/i) == 'micromessenger') {
		return true;
	} else {
		return false;
	}
}

export function heightToTop(element) {
	//ele为指定跳转到该位置的DOM节点
	let root = document.body;
	let height = 0;
	do {
		height += element.offsetTop;
		element = element.offsetParent;
	} while (element !== root)
	return height;
}

/**
 * 滚动条到最下方
 * @param id 容器id
 */
export function scrollBottom(id) {
	let div = document.getElementById(id);
	if(div) {
		setTimeout(() => {
			div.scrollTop = div.scrollHeight;
		}, 0)
	}
}